import { setBotSwitch } from '@/api/new'
import useErrorMessage from '@/components/storybook/hooks/useErrorMessage'
import CloseSwitchModal from '@/components/storybook/b-switch-message/close-switch-modal'

export default {
  components: {
    CloseSwitchModal
  },
  data() {
    return {
      closeSwitchVisible: false,
      closeSwitchModalType: undefined,
      currentSwitchRow: {}
    }
  },
  methods: {
    async toggleTrigger(item, value, callback) {

      try {
        this.$set(item, '_loading', true)
        const { enable, alert, escalation, id } = item

        const calert = enable ? false : alert
        const cescalation = enable ? false : escalation

        const params = {
          id,
          enable: !enable,
          alert: calert,
          escalation: cescalation,
          mode: 'enable',
          tenantLog: {
            operationGroup: 0,
            operationType: 1,
            operation: {
              enable: !enable,
              alert: calert,
              escalation: cescalation,
              mode: 'enable'
            }
          }
        }
        const { data: response } = await setBotSwitch(params)
        if (response.code === 418 || response.code === 523) {
          return this.$emit('open-pay')
        }
        if (response.code === 518) {
          this.showNotice = true
        }

        if (response.code !== 0) {
          throw response.msg
        }

        if (enable) {
          this.clearBot(item)
        }

        this.$set(item, 'enable', !enable)
        this.$set(item, 'alert', calert)
        this.$set(item, 'escalation', cescalation)

        if (callback) {
          callback()
        }
      } catch (error) {
        // useErrorMessage(error)
      } finally {
        this.$set(item, '_loading', false)
      }
    },
    async toggleAlert(item, value, callback) {
      try {
        this.$set(item, '_loading', true)
        const { enable, alert, escalation, id } = item

        const cescalation = alert ? false : escalation

        const params = {
          id,
          enable,
          alert: !alert,
          escalation: cescalation,
          mode: 'alert',
          tenantLog: {
            operationGroup: 0,
            operationType: 1,
            operation: {
              enable,
              alert: !alert,
              escalation: cescalation,
              mode: 'alert'
            }
          }
        }
        const { data: response } = await setBotSwitch(params)
        if (response.code === 418 || response.code === 523) {
          return this.$emit('open-pay')
        }

        if (response.code !== 0) {
          throw response.msg
        }
        this.$set(item, 'alert', !alert)
        this.$set(item, 'escalation', cescalation)

        if (callback) {
          callback()
        }
      } catch (error) {
        useErrorMessage(error)
      } finally {
        this.$set(item, '_loading', false)
      }
    },
    async toggleEscalate(item, value, callback) {
      try {
        this.$set(item, '_loading', true)
        const { enable, alert, escalation, id } = item

        const params = {
          id,
          enable,
          alert,
          escalation: !escalation,
          mode: 'escalation',
          tenantLog: {
            operationGroup: 0,
            operationType: 1,
            operation: {
              enable,
              alert,
              escalation: !escalation,
              mode: 'escalation'
            }
          }
        }
        const { data: response } = await setBotSwitch(params)
        if (response.code === 418 || response.code === 523) {
          return this.$emit('open-pay')
        }

        if (response.code !== 0) {
          throw response.msg
        }
        this.$set(item, 'escalation', !escalation)

        if (callback) {
          callback()
        }
      } catch (error) {
        useErrorMessage(error)
      } finally {
        this.$set(item, '_loading', false)
      }
    },
    openCloseSwitchModal(item, type) {
      this.currentSwitchRow = item
      this.closeSwitchModalType = type
      this.closeSwitchVisible = true
    },
    async onSuccessSwicthModal() {
      if (this.closeSwitchModalType === 'alert') {
        await this.toggleAlert(this.currentSwitchRow)
      }
      if (this.closeSwitchModalType === 'escalation') {
        await this.toggleEscalate(this.currentSwitchRow)
      }
      this.currentSwitchRow = {}
      this.closeSwitchModalType = undefined
    },
    onSwitchModalCancel() {
      this.currentSwitchRow = {}
      this.closeSwitchModalType = undefined
    },
    async clearBot(item) {
      try {
        const { closeBot } = this.$interface.pay
        const { id } = item || {}
        const params = {
          botId: id
        }
        await closeBot(params)
      } catch (error) {}
    }
  }
}
